# coding=utf-8

import datetime
import string
import random
from flask import make_response
import json
import requests
from bs4 import BeautifulSoup as bs
from validate_email import validate_email


letters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
digits = '0123456789'


class JSONEncoder(json.JSONEncoder):
    def encode(self, o):
        from over.model import db
        if isinstance(o, db.Model):
            return json_dumps(o.to_dict())

        if isinstance(o, datetime.datetime):
            return o.strftime('%Y-%m-%d %H:%M:%S')
        return json.JSONEncoder.encode(self, o)


def json_dumps(v, **kwargs):
    kwargs['cls'] = JSONEncoder
    return json.dumps(v, **kwargs)

def json_loads(v):
    return json.loads(v)


def is_valid_email(email):
    return validate_email(email)


def notify_new_user(email):
    pass


def jsonify(v, pretty=False):
    if pretty:
        v = json_dumps(v, indent=4)
    else:
        v = json_dumps(v)
    resp = make_response(v)
    resp.headers['Content-Type'] = 'application/json'
    return resp


def random_str(length=10):
    return ''.join([random.choice(letters) for i in range(length)])


def random_digital(length=5):
    return ''.join([random.choice(digits) for i in range(length)])


def random_printer(length=10):
    choice = digits + letters + '<=>?@#$%&^_`{|}~'
    return ''.join([random.choice(choice) for i in range(length)])

def new_order_number():
    n = datetime.datetime.now()
    # 5位随机数
    return n.strftime('%Y%m%d%H%M%S%f') + random_digital()


def new_pay_number():
    return new_order_number()


def alipay_fill(url):
    # 支付宝页面填充,然后跳转到支付宝页面
    # url = app.config['ALIPAY_PATH']
    data = {}
    headers = {
        'Cookie': 'JSESSIONID=GZ00hWax9Xm8rvSYjMj5oNhHFbEMaLshenghuoGZ00; cna=VnAcEPanOAwCAS0/e+I5ICb1; l=Av//gQt/Q4AjC-10AW1cxsujD9mJ1lOG; isg=AqGhnHWvUuiBAf4cXVa8bxQvsGuQPhVAFZw5xQN2r6gHasA8S5xXEp7quimW; NEW_ALIPAY_TIP=1; unicard1.vm="K1iSL1gn5tJnKx5epbBmbw=="; JSESSIONID=215F4165E8F144A0E054A82D6A46F1F7; session.cookieNameId=ALIPAYJSESSIONID; mobileSendTime=-1; credibleMobileSendTime=-1; ctuMobileSendTime=-1; riskMobileBankSendTime=-1; riskMobileAccoutSendTime=-1; riskMobileCreditSendTime=-1; riskCredibleMobileSendTime=-1; riskOriginalAccountMobileSendTime=-1; ctoken=tGs6423A0Hd6voMQ; LoginForm=alipay_login_auth; alipay=K1iSL1gn5tJnKx5epbBmbxlkIqVN+VBboBYOHhnYGhMkQzo9; CLUB_ALIPAY_COM=2088102963584721; iw.userid="K1iSL1gn5tJnKx5epbBmbw=="; ali_apache_tracktmp="uid=2088102963584721"; zone=RZ13A; ALIPAYJSESSIONID=RZ13Sf9Oz23zUitz9LL4t5RVbVdhQqauthRZ13GZ00; spanner=dCqJ9Hl4R16YBPeL1x6AIs+Q35WIsXnGXt2T4qEYgj0=',
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36',
    }
    resp = requests.post(url, data, headers=headers)
    print(resp.url)

    content = resp.content.decode('GBK')
    soup = bs(content, "lxml")

    soup.find_all("#paymentForm")

    pay_form = soup.find("#paymentForm")

    print(pay_form)


if __name__ == '__main__':
    url = 'https://shenghuo.alipay.com/send/payment/fill.htm'
    alipay_fill(url)
